package com.init.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.init.common.BaseResponse;
import com.init.common.ErrorCode;
import com.init.common.ResultUtils;
import com.init.exception.BusinessException;
import com.init.model.entity.Graders;
import com.init.service.GradersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 评定人接口
 * @author Atanycosts
 * @date 2025/1/4
 */
@RestController
@Slf4j
@RequestMapping("/grade")
public class GraderController {

    @Resource
    private GradersService gradersService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    @PreAuthorize("hasRole('ADMIN') or hasRole('BOSS')") //用户权限
    public BaseResponse<Page> list(int page, int pageSize, String userAccount) {
        // 已排查
        Page pageBaseResponse = gradersService.queryPage(page, pageSize, userAccount);

        if (pageBaseResponse.getSize() == 0) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "数据异常,可能为空");
        }
        return ResultUtils.success(pageBaseResponse);
    }


    /**
     * 根据ID查询
     */
    @GetMapping("/{gradeId}")
    public BaseResponse<Graders> info(@PathVariable("gradeId") Integer gradeId) {
        // 已排查
        Graders gradesByID = gradersService.getById(gradeId);

        if (gradesByID == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "请求数据为空");
        }
        return ResultUtils.success(gradesByID);
    }



    /**
     * 修改
     */
    @PatchMapping("/update")
    @Transactional
    public BaseResponse<String> update(@RequestBody Graders graders) {
        // 已排查
        boolean b = gradersService.updateById(graders);

        if (b) {
            return ResultUtils.success("修改成功");
        }
        throw new BusinessException(ErrorCode.OPERATION_ERROR, "修改失败");
    }

}
